import { addMenuApi, delMenuApi, editMenuApi } from '@/api/system/menu/menu'
import { AddMenuModel, MenuModel, MenuParam } from '@/api/system/menu/MenuModel'
import useInstance from '@/hooks/useInstance'
import { Result, StatusCode } from '@/http/request'
import { EditType } from '@/types/BaseEnum'
import { reactive, ref } from 'vue'

export default function useMenu(getMenuTable: any) {
    const { global } = useInstance()

    const addEditRef = ref<{ show: (type: string, row?: MenuModel) => void }>()

    const addModel = reactive<MenuModel>({
        id: 0,
        editType: '',
        type: '',
        parent_id: '',
        path: '',
        name: '',
        hidden: '0',
        component: '',
        title: '',
        sort: 0,
        icon: '',
        code: '',
        keep_alive: '1',
        is_link: '0',
        status: '1'
    })

    // 新增
    const addBtn = (type: string, parentId: string) => {
        if (type == '0') {
            addModel.type = '1'
        } else if (type == '1') {
            addModel.type = '2'
        } else {
            addModel.type = '0'
        }
        addModel.component = 'Layout'
        addModel.parent_id = parentId
        addModel.is_link = '0'
        addModel.keep_alive = '1'
        addEditRef.value?.show(EditType.ADD, addModel)
    }

    // 编辑
    const editBtn = async (row: MenuModel) => {
        row.type = row.type.toString()
        row.keep_alive = row.keep_alive ? '1' : '0'
        row.is_link = row.is_link ? '1' : '0'
        row.hidden = row.hidden ? '1' : '0'
        row.status = String(row.status)
        addEditRef.value?.show(EditType.EDIT, row)
    }

    // 删除
    const delBtn = async (id: number) => {
        const confirm = await global.$eleConfirm('确定删除该数据吗？')
        if (confirm) {
            // 执行删除操作
            let res = await delMenuApi(
                reactive<MenuParam>({
                    id: id
                })
            )
            if (res && res.code == StatusCode.Success) {
                // 刷新列表
                getMenuTable()
                // 信息提示
                global.$message({ message: res.msg, type: 'success' })
            }
        }
    }

    // 保存
    const save = async (param: MenuModel) => {
        const menuParam = reactive<AddMenuModel>({
            id: param.id,
            editType: param.editType,
            type: parseInt(param.type),
            parent_id: parseInt(param.parent_id),
            path: param.path,
            name: param.name,
            hidden: parseInt(param.hidden),
            component: param.component,
            title: param.title,
            sort: param.sort,
            icon: param.icon,
            code: param.code,
            keep_alive: parseInt(param.keep_alive),
            is_link: parseInt(param.is_link),
            status: parseInt(param.status)
        })
        console.log('保存', menuParam)
        let res: Result
        if (param.editType == EditType.ADD) {
            res = await addMenuApi(menuParam)
        } else {
            res = await editMenuApi(menuParam)
        }
        if (res && res.code == StatusCode.Success) {
            // 刷新列表
            getMenuTable()
            // 信息提示
            global.$message({ message: res.msg, type: 'success' })
        }
    }
    return {
        addBtn,
        editBtn,
        delBtn,
        save,
        addEditRef
    }
}
